package top.eleven.sixpence.generaterollbacksql.convertor;

import top.eleven.sixpence.generaterollbacksql.annotation.Convertor;
import top.eleven.sixpence.generaterollbacksql.common.CommonConstant;
import top.eleven.sixpence.generaterollbacksql.common.SQLKey;

/**
 * 表明变更转换器
 *
 * @author Eleven
 */
@Convertor(sqlPrefix = {SQLKey.RENAME_C, SQLKey.RENAME_L})
public class RenameConvertor extends AlertTableConvertor{
    @Override
    public String doConvert(String table, String sql) {
        String newTable = sql.replace(SQLKey.RENAME_TO_C, CommonConstant.EMPTY_STRING)
                .replace(SQLKey.RENAME_TO_L, CommonConstant.EMPTY_STRING)
                .replace(SQLKey.RENAME_C, CommonConstant.EMPTY_STRING)
                .replace(SQLKey.RENAME_L, CommonConstant.EMPTY_STRING)
                .replace(CommonConstant.SEMICOLON,CommonConstant.EMPTY_STRING)
                .trim();

        StringBuilder sb = new StringBuilder(SQLKey.ALTER_TABLE_C);
        sb.append(CommonConstant.ONE_BLANK)
                .append(newTable)
                .append(CommonConstant.ONE_BLANK)
                .append(SQLKey.RENAME_TO_C)
                .append(CommonConstant.ONE_BLANK)
                .append(table)
                .append(CommonConstant.SEMICOLON)
                .append(CommonConstant.NEW_LINE);

        return sb.toString();
    }
}
